package com.spring.dao;

import com.spring.entity.Account;

import com.spring.utils.ConnectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.util.List;


public class AccountDaoImpl implements AccountDao {

    private QueryRunner queryRunner;
    private ConnectionUtils connectionUtils;

    public Account findAccountByName(String accountName) {
        try{

            List<Account> query = queryRunner.query(connectionUtils.getThreadConnection()," select id,account_name,money from ac where account_name = ?", new BeanListHandler<Account>(Account.class), accountName);
        if(query == null || query.size() == 0){
            return null;
        }
        if(query.size() >= 2){
            throw  new RuntimeException("数据大约2条");
        }
            Account account = query.get(0);
            account.setAccountName(accountName);
       return  account;
        }catch (Exception e){
            throw  new RuntimeException(e);
        }
    }

    public void updateAccount(Account account) {
        try{
          queryRunner.update(connectionUtils.getThreadConnection(),"update ac set money = ? where account_name = ?", account.getMoney(), account.getAccountName());
        }catch (Exception e){
            throw  new RuntimeException(e);
        }
    }

    public QueryRunner getQueryRunner() {
        return queryRunner;
    }

    public void setQueryRunner(QueryRunner queryRunner) {
        this.queryRunner = queryRunner;
    }

    public ConnectionUtils getConnectionUtils() {
        return connectionUtils;
    }

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }
}
